package zhongbo.java.air.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.monitor.model.FlightActivityDaily;
import zhongbo.java.air.monitor.service.IFlightActivityDailyService;

import java.util.Date;
import java.util.List;

@Service
public class FlightActivityDailyServiceImpl implements IFlightActivityDailyService {
    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {
        return flightActivityDailyMapper.selectDailyStats(date);
    }


    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
//        if (StrUtil.isBlank(date)) {
//            throw new RuntimeException("data参数必须填写!示例: 2025-04-08");
//        }
        // todo 根据 date 条件, 从 air_flight_activity_daily 表里查询结果,并返回

        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(date)) {
            queryWrapper.eq("date",date);
        }

        List<FlightActivityDaily> dailyStatsList = flightActivityDailyMapper.selectList(queryWrapper);
        return dailyStatsList;
    }


    /* 以下复用crud代码 */
    @Override
    public String addFlightActivityDaily(FlightActivityDaily flightActivityDaily) {
        if (flightActivityDaily == null) {
            throw new RuntimeException("航迹记录对象为空");
        }

        int insert = flightActivityDailyMapper.insert(flightActivityDaily);
        if (insert > 0) {
            return flightActivityDaily.getId();
        }
        throw new RuntimeException("新增航迹记录失败");
    }

    @Override
    public FlightActivityDaily updateFlightActivityDaily(FlightActivityDaily flightActivityDaily) {
        if (flightActivityDaily == null) {
            throw new RuntimeException("航迹对象为空");
        }

        int update = flightActivityDailyMapper.updateById(flightActivityDaily);
        if (update > 0) {
            return flightActivityDaily;
        }
        throw new RuntimeException("修改航迹记录失败");
    }

    @Override
    public int deleteFlightActivityDaily(String airId) {
        if (airId == null) {
            throw new RuntimeException("未找到航迹对象id");
        }

        int delete = flightActivityDailyMapper.deleteById(airId);
        if (delete > 0) {
            return delete;
        }
        throw new RuntimeException("删除航迹记录失败");
    }

    @Override
    public FlightActivityDaily getFlightActivityDailyById(String airId) {
        if (airId == null) {
            throw new RuntimeException("未找到航迹对象id");
        }
        return flightActivityDailyMapper.selectById(airId);
    }

//    @Override
//    public List<FlightActivityDaily> getFlightActivityDailyAll() {
//        return flightActivityDailyMapper.getAirFlightPathAll();
//    }

    @Override
    public PageVo<FlightActivityDaily> page(@RequestParam(required = false) String icaoCode,
                                      @RequestParam(defaultValue = "1") int pageNumber,
                                      @RequestParam(defaultValue = "1") int pageSize) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(icaoCode)) {
            queryWrapper.like("icao_code",icaoCode);
        }
        IPage pageParam = new Page(pageNumber,pageSize);
        IPage airFlightPage = flightActivityDailyMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airFlightPage);
    }

//    @Override
//    public PageVo<FlightActivityDaily> pageTimeFlightPathAll(String icao, Date startTime, Date endTime, Integer pageNumber, Integer pageSize) {
//        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper();
//        if (StrUtil.isNotBlank(icao)) {
//            queryWrapper.like("icao_code",icao).between("timestamp",startTime,endTime);
//        }
//        IPage pageParam = new Page(pageNumber,pageSize);
//        IPage airIngestion = flightActivityDailyMapper.selectPage(pageParam,queryWrapper);
//        return new PageVo<>(airIngestion);
//    }


}
